package com.fg.cloud.agency.controller.system;

import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fg.cloud.agency.controller.BaseAgencyController;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.manager.entry.factory.FactoryNewsEntity;
import com.fg.cloud.manager.entry.factory.vo.FactoryNewsVo;
import com.fg.cloud.manager.service.factory.FactoryNewsService;

/**
 * Description:经销商查看新闻
 * @author dingjiang
 * @date 2017年8月22日下午4:28:55
 */
@Controller
@RequestMapping("/agencyNews")
public class AgencyNewsController extends BaseAgencyController{
	
	@Autowired
	private FactoryNewsService factoryNewsService;

	/**
	 * Description:经销商用户查看新闻公告（兼容游客访问）
	 * @param req
	 * @param resp
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param beginTime
	 * @param endTime
	 * @param type
	 * @author dingjiang
	 * @date 2017年9月19日下午5:26:42
	 */
	@RequestMapping(value="/agencySelect",method={RequestMethod.GET,RequestMethod.POST})
	@ResponseBody
	public void agencySelect(HttpServletResponse resp, Integer page, Integer rows, 
			String sort, String order, FactoryNewsVo entity){
		try {
			if (page == null) {
				page = 1;
			}
			if (rows == null) {
				rows = 10;
			}
			Long facId = getCurrentAgencyUser().getFacId();
			if (null == facId) {
				super.loginOut(resp);
				return;
			}
			
			Long ageId = getCurrentAgencyUser().getAgeId();
			PageUtils<FactoryNewsEntity> pageinfo = new PageUtils<FactoryNewsEntity>(page, rows, sort, order);
			Map<String, Object> condition = pageinfo.getCondition();
			if ( LongUtils.isEmpty(ageId) ) {
				condition.put("facId", facId);
			} else {
				condition.put("levelId", getCurrentAgencyUser().getLevelId());
				condition.put("areaId", getCurrentAgencyUser().getAreaId());
				condition.put("ageId", ageId);
				condition.put("facId", facId);
			}
			condition.put("beginTime", entity.getBeginTime());
			condition.put("endTime", entity.getEndTime());
			condition.put("type", entity.getType());
			
			condition.put("newsTitle", entity.getNewsTitle());

			//处理全局游客模式时，只查询当前厂商全部新闻
			if (null == ageId) condition.put("reciverType", 0);
			
			factoryNewsService.selectPage(pageinfo);
			doResult(resp, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(resp, false, StatusCode.FINDERROR, null, e.getMessage());
		}
	}
}
